﻿using System;
using System.Collections.Generic;
using System.Linq;
using Common;
using System.Data;
namespace DAL
{
    using DAL.Framework;

    /// <summary>
    /// 角色
    /// </summary>
    public class SysRoleRepository : BaseRepository,IDisposable 
    {
       
        /// <summary>
        /// 获取所有角色
        /// </summary>
        /// <returns>角色集合</returns>
        public IQueryable<SysRole> GetAll()
        {
            return DbContextFactory.DbContext.SysRole.AsQueryable();
        }
        /// <summary>
        /// 导出Flexigrid中查询的数据
        /// </summary>
        /// <param name="param">客户端传进的数据</param>
        // <param name="listQuery">额外的参数</param>
        /// <returns></returns>      
        public IQueryable<SysRole> DaoChuData( FlexigridParam param, params object[] listQuery)
        {
            string where = string.Empty;
            int flagWhere = 0;

            Dictionary<string, string> queryDic = ValueConvert.StringToDictionary(param.Query.GetString());
            if (queryDic != null && queryDic.Count > 0)
            {
                foreach (var item in queryDic)
                {
                    if (flagWhere != 0)
                    {
                        where += " and ";
                    }
                    flagWhere++;                  
                    
                    if (queryDic.ContainsKey("SysPerson")&& !string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key == "SysPerson")
                    {//查询多对多关系的列名
                        where += "EXISTS(select 1 from SysRoleSysPerson as p where p.SysPersonId='" + item.Value + "')";
                        continue;
                    }
                    if (queryDic.ContainsKey("SysMenu")&& !string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key == "SysMenu")
                    {//查询多对多关系的列名
                        where += "EXISTS(select 1 from SysMenuSysRole as p where p.SysMenuId='" + item.Value + "')";
                        continue;
                    }
                    if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key.Contains(Start_Time)) //需要查询的列名
                     {
                         where += "it. " + item.Key.Remove(item.Key.IndexOf(Start_Time)) + " >=  CAST('" + item.Value + "' as   System.DateTime)";
                         continue;
                     }
                     if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key.Contains(End_Time)) //需要查询的列名
                     {
                         where += "it." + item.Key.Remove(item.Key.IndexOf(End_Time)) + " <  CAST('" + Convert.ToDateTime(item.Value).AddDays(1) + "' as   System.DateTime)";
                         continue;
                     }
                     if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key.Contains(Start_Int)) //需要查询的列名
                     {
                         where += "it." + item.Key.Remove(item.Key.IndexOf(Start_Int)) + " >= " + item.Value.GetInt();
                         continue;
                     }
                     if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key.Contains(End_Int)) //需要查询的列名
                     {
                         where += "it." + item.Key.Remove(item.Key.IndexOf(End_Int)) + " <= " + item.Value.GetInt();
                         continue;
                     }

                     if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key.Contains(End_String)) //需要查询的列名
                     {
                         where += "it." + item.Key.Remove(item.Key.IndexOf(End_String)) + " = '" + item.Value +"'";
                         continue;
                     }

                     if (!string.IsNullOrWhiteSpace(item.Key) && !string.IsNullOrWhiteSpace(item.Value) && item.Key.Contains(DDL_String)) //需要查询的列名
                     {
                         where += "it." + item.Key.Remove(item.Key.IndexOf(DDL_String)) + " = '" + item.Value +"'";
                         continue;
                     }
                    where += "it." + item.Key + " like '%" + item.Value + "%'";
                }
            }
            return DbContextFactory.DbContext.SysRole.SqlQuery("select * from SysRole  it " + (string.IsNullOrEmpty(where) ? "" : "where " + where) + " order by  it." + param.SortName.GetString() + " " + param.SortOrder.GetString()).AsQueryable(); 
        }
       
        /// <summary>
        /// 通过主键id，获取角色---查看详细，首次编辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>角色</returns>
        public SysRole GetById( string id)
        { 
                 return DbContextFactory.DbContext.SysRole.SingleOrDefault(s => s.Id == id); 
        }
        
      
        /// <summary>
        /// 获取在该表一条数据中，出现的所有外键实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>外键实体集合</returns>
        public IQueryable<SysPerson> GetRefSysPerson( string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                return from m in DbContextFactory.DbContext.SysRole
                       from f in m.SysPerson
                       where m.Id == id
                       select f;
            }
            return null;
        }
        /// <summary>
        /// 获取在该表中出现的所有外键实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>外键实体集合</returns>
        public IQueryable<SysPerson> GetRefSysPerson()
        {
            return from m in DbContextFactory.DbContext.SysRole
                   from f in m.SysPerson
                   select f;
        }
      

       
        /// <summary>
        /// 获取在该表一条数据中，出现的所有外键实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>外键实体集合</returns>
        public IQueryable<SysMenu> GetRefSysMenu( string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                return from m in DbContextFactory.DbContext.SysRole
                       from f in m.SysMenu
                       where m.Id == id
                       select f;
            }
            return null;
        }
        /// <summary>
        /// 获取在该表中出现的所有外键实体
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>外键实体集合</returns>
        public IQueryable<SysMenu> GetRefSysMenu()
        {
            return from m in DbContextFactory.DbContext.SysRole
                   from f in m.SysMenu
                   select f;
        }
      
        /// <summary>
        /// 创建一个角色
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="entity">将要创建的一个角色</param>
        public void Create(SysRole entity)
        {
            if (entity != null)
            { 
                DbContextFactory.DbContext.SysRole.Add(entity);
             }             
        }
       
        /// <summary>
        /// 创建角色对象集合
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="entitys">对象集合</param>
        public void Create( IQueryable<SysRole> entitys)
        {
            foreach (var entity in entitys)
            {
                this.Create(entity);
            }
        }

        /// <summary>
        /// 删除一个角色
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="id">一条角色的主键</param>
        public void Delete(string id)
        {
            SysRole deleteItem = GetById(id);
            if (deleteItem != null)
            { 
                DbContextFactory.DbContext.SysRole.Remove(deleteItem);
            }
        }
        /// <summary>
        /// 删除对象集合
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="deleteCollection">主键的集合</param>
        public void Delete( string[] deleteCollection)
        {
            //数据库设置级联关系，自动删除子表的内容   
            IQueryable<SysRole> collection = from f in DbContextFactory.DbContext.SysRole
                    where deleteCollection.Contains(f.Id)
                    select f;
            foreach (var deleteItem in collection)
            {
                DbContextFactory.DbContext.SysRole.Remove(deleteItem);
            }
        }
        /// <summary>
        /// 编辑一个角色对象
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="entity">将要编辑的一个角色对象</param>
        public SysRole Edit( SysRole entity)
        {            
            DbContextFactory.DbContext.SysRole.Attach(entity);
            DbContextFactory.DbContext.Entry(entity).State=EntityState.Modified;         
            return entity;
        }
        /// <summary>
        /// 编辑角色对象集合
        /// </summary>
        /// <param name="db">实体数据</param>
        /// <param name="entitys">对象集合</param>
        public void Edit( IQueryable<SysRole> entitys)
        {
            foreach (SysRole entity in entitys)
            {
                this.Edit(entity);
            }
        }

        public void Dispose()
        {            
        }
    }
}

